const express = require('express');
const path = require('path');
const fs = require('fs');
const cors=require('cors')
const app = express();
app.use(cors()); 
const port = 3001; // 你可以根据需要设置不同的端口号
app.use(express.static(path.join(__dirname, 'public')));
app.get('/getMusic', (req, res) => {
  const coverDir = path.join(__dirname, 'public/cover');
  const audioDir=path.join(__dirname, 'public/audio');
  const imageFiles = fs.readdirSync(coverDir)
  const audioFiles=fs.readdirSync(audioDir);
  const titles=[]
  audioFiles.forEach((file)=>{
    const filename=path.basename(file);
    const title=filename.split('.')[0]
    titles.push(title)
    
  })
  
  const imageList = imageFiles.map(file => `http://192.168.101.3:${port}/cover/${file}`); 
  const audioList=audioFiles.map(file=>`http://192.168.101.3:${port}/audio/${file}`);

 

  res.json({
      imageList,
      audioList,
      titles
  });


});

app.listen(port, () => {
  console.log(`服务器在端口 ${port} 上运行`);
});
const os = require('os');
//获取本机ip
let getIPAdress = function() {
    var interfaces = os.networkInterfaces();
    for (var devName in interfaces) {
        var iface = interfaces[devName];
        for (var i = 0; i < iface.length; i++) {
            var alias = iface[i];
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                return alias.address;
            }
        }
    }
}
let ip = getIPAdress();
console.log(ip);
